#!/usr/bin/python
# -*- coding: utf-8 -*-
import omencore as ft
import os


def test_read_file():
    ''' read file test'''
    text = ft.FileTools.read_text('file/test.conf')
    lines = text.splitlines()
    assert lines[0] == '{'
    assert lines[1] == '\t"key1": "value1",'
    assert lines[2] == '\t"key2": "value2"'
    assert lines[3] == '}'
    
def test_read_file_error():
    '''test file read exception'''
    try:
        text = ft.FileTools.read_text('toto')
        assert False
        print text
    except IOError:
        assert True
        
def test_write_and_delete_file():
    '''test write and delete file'''
    text = "aaaa"
    ft.FileTools.save_text(text, 'file/test.txt')
    text2 = ft.FileTools.read_text('file/test.txt')
    assert text == text2
    ft.FileTools.delete_file('file/test.txt')
    assert os.path.exists('file/test.txt') == False
    
def test_is_file_present():
    ''' test is file present '''
    ft.FileTools.delete_file('file/test2.txt', raise_exception=False)
    assert ft.FileTools.is_file_present('file/test2.txt') == False
    ft.FileTools.save_text('aaaa', 'file/test2.txt')
    assert ft.FileTools.is_file_present('file/test2.txt') == True
    ft.FileTools.delete_file('file/test2.txt')
    assert ft.FileTools.is_file_present('file/test2.txt') == False
    
    
def test_parse():
    assert ft.ParseTools.str_to_boolean('1') == True
    assert ft.ParseTools.str_to_boolean('true') == True
    assert ft.ParseTools.str_to_boolean('True') == True
    assert ft.ParseTools.str_to_boolean('False') == False
    assert ft.ParseTools.str_to_boolean('other') == False
    assert ft.ParseTools.str_to_boolean(None) == False
    
